import React from "react";
import { View, Platform } from "react-native";
import { RNCamera } from "react-native-camera";
import PropTypes from "prop-types";
import QRScannerRectView from "../QRScannerRectView";

export default class ScannerScreen extends React.Component {
  static propTypes = {
    rectStyle: PropTypes.object,

    cornerStyle: PropTypes.object,

    scanBarAnimateTime: PropTypes.number,
    scanBarAnimateReverse: PropTypes.bool,
    scanBarStyle: PropTypes.object,

    hintTextStyle: PropTypes.object,
  };

  static defaultProps = {
    hintTextStyle: null,
    scanBarStyle: null,
    scanBarAnimateReverse: false,
    scanBarAnimateTime: 0,
    rectStyle: null,
    cornerStyle: null,
  };

  state = {
    isFlashOn: false,
  };

  setFlash = isFlashOn => {
    console.log("isFlashOn:", isFlashOn);
    this.setState({ isFlashOn });
  };

  render() {
    const { onScanResult } = this.props;
    const {
      hintTextStyle,
      maskColor,
      scanBarAnimateTime,
      rectStyle,
      cornerStyle,
      scanBarStyle,
      hintText,
      scanBarAnimateReverse = false,
    } = this.props;
    const { isFlashOn } = this.state;
    let scanView = null;
    if (Platform.OS === "ios") {
      scanView = (
        <RNCamera
          style={{
            flex: 1,
          }}
          flashMode={
            isFlashOn
              ? RNCamera.Constants.FlashMode.on
              : RNCamera.Constants.FlashMode.off
          }
          // useCamera2Api={false}
          // torchMode={isFlashOn
          //   ? RNCamera.Constants.TorchMode
          //   : RNCamera.Constants.FlashMode.off}
          // /* 相机闪光模式*/
          type={RNCamera.Constants.Type.back}
          barCodeTypes={[RNCamera.Constants.BarCodeType.qr]}
          // flashMode={RNCamera.Constants.FlashMode.auto}
          onBarCodeRead={e => onScanResult(e)}
        >
          {/* 绘制扫描遮罩 */}
          <QRScannerRectView
            maskColor={maskColor}
            rectStyle={rectStyle}
            cornerStyle={cornerStyle}
            hintText={hintText}
            setFlash={this.setFlash}
            scanBarAnimateTime={scanBarAnimateTime}
            scanBarAnimateReverse={scanBarAnimateReverse}
            scanBarStyle={scanBarStyle}
            hintTextStyle={hintTextStyle}
          />
        </RNCamera>
      );
    } else {
      scanView = (
        <RNCamera
          style={{
            flex: 1,
          }}
          type={RNCamera.Constants.Type.back}
          googleVisionBarcodeType={
            RNCamera.Constants.GoogleVisionBarcodeDetection.BarcodeType.QR_CODE
          }
          flashMode={
            isFlashOn
              ? RNCamera.Constants.FlashMode.on
              : RNCamera.Constants.FlashMode.off
          }
          useCamera2Api={false}
          onBarCodeRead={e => onScanResult(e)}
        >
          {/* 绘制扫描遮罩 */}
          <QRScannerRectView
            maskColor={maskColor}
            rectStyle={rectStyle}
            setFlash={this.setFlash}
            cornerStyle={cornerStyle}
            scanBarAnimateReverse={scanBarAnimateReverse}
            scanBarAnimateTime={scanBarAnimateTime}
            hintTextStyle={hintTextStyle}
          />
        </RNCamera>
      );
    }

    return (
      <View
        style={{
          flex: 1,
        }}
      >
        {scanView}
      </View>
    );
  }
}
